#include "qxmms.h"

int qxmms_test() {
	if(xmms_remote_get_version(XMMS_SESSION)) {
		return 1;
	} else {
		return 0;
	}
}
	
int qxmms_process(int argc, char **args) {

	int startloop = 0;

	if(!qxmms_test()) {
		if(args[1] && !strcmp(args[1],"--start")) {
			system("xmms &> /dev/null &");
			while(!qxmms_test()) {
				if(startloop >= 40) { // 40 iterations at ~200ms/iter ~8 seconds.
					printf("Unable to start XMMS.\n");	
					return 1;
				}		
				usleep(200000);
				startloop++;
			}
			usleep(250000); //. give it some time to breath,
		} else if(args[1] && !strcmp(args[1],"--version")) {
			qxmms_version();
			return 0;
		} else if(args[1] && !strcmp(args[1],"-h")) {
			qxmms_help();
			return 0;
		} else {
			printf("XMMS does not seem to be running.\nUse `--start` to launch it.\n");
			return 0;
		}
	}

	//. random mostly temp variables.
	int a,b,c;

	//. processing variables.
	int switchblock = FALSE;
	int command = FALSE;
	char cmdval[512] = ""; //. ended up making this longer for filepaths on addfile.
	
	//. init default settings.
	p_time = TRUE;
	r_labels = FALSE;
	r_help = FALSE;
	r_ver = FALSE;
	r_quotes = FALSE;
	s_volume = FALSE;
	
	//. find the switch block.
	for(a = 1; a < argc; a++) {
		if(args[a][0] == 45 && (args[a][1] != 45 && (args[a][1] < '0' || args[a][1] > '9'))) {
			switchblock = a;
			break;
		}
	}
	
	//. run through all the args for commands.
	for(a = 1; a < argc; a++) {
		for(b = 0; b < (sizeof(cmd) / sizeof(cmd_s)); b++) {
			if(a != switchblock && !strcmp(args[a],cmd[b].name)) {
				if(cmd[b].value) {
					for(c = 1; c <= cmd[b].value; c++) {
						if((a + c) < argc && (a + c) != switchblock) {
							if(c > 1) {
								strcat(cmdval," ");
							}
							strcat(cmdval,args[a+c]);
						}
					}
				} else {
					cmdval[0] = NUL;
				}
				cmd[b].func(cmdval);
				cmdval[0] = NUL;
				command = TRUE;
				break;
			}
		}
	}
	
	if(command && !switchblock) {
		//. command with no switches, might as well quit.
		return 0;
	} else if(command) {
		//. give xmms time to do something before querying it. 250ms.
		usleep(250000);	
	}
	
	//. run through the switch block once for render modifier switches.
	if(switchblock) {
		for(a = 1; a < strlen(args[switchblock]); a++) {
			switch(args[switchblock][a]) {
				case(SWITCH_LABELS): {
					r_labels = TRUE;
					break;
				}
				case(SWITCH_GIVE_SECONDS): {
					p_time = FALSE;
					break;
				}
				case(SWITCH_QUOTE_STRINGS): {
					r_quotes = TRUE;
					break;
				}
				case(SWITCH_HELP): {
					r_help = TRUE;
					break;
				}
				case(SWITCH_VERSION): {
					r_ver = TRUE;
					break;
				}
			}	
		}
	}
	
	if(r_ver || r_help) {
		qxmms_version();
	}
	if(r_help) {
		qxmms_help();
	}
	if(r_ver || r_help) {
		return 0;
	}
	
	//. query xmms.
	qxmms(&q);
	
	
	//. run through the switchblock again for last second renders, printing
	//. the output in the order of the switches.
	if(switchblock) {
		for(a = 1; a < strlen(args[switchblock]); a++) {

			//. no printing for modifiers.
			if(args[switchblock][a] == SWITCH_QUOTE_STRINGS
			|| args[switchblock][a] == SWITCH_LABELS
			|| args[switchblock][a] == SWITCH_HELP
			|| args[switchblock][a] == SWITCH_VERSION
			|| args[switchblock][a] == SWITCH_GIVE_SECONDS) {
				continue;
			}
			
			//. if this is not the first, add a space.
			if(a > 1) { printf(" "); }
			
			//. start quote.
			if(r_quotes) { printf("\""); }
			
			//. test switch.
			switch(args[switchblock][a]) {
				case(SWITCH_TITLE): {
					qxmms_title();
					break;
				}
				case(SWITCH_SAMPLERATE): {
					qxmms_samplerate();
					break;
				}
				case(SWITCH_BITRATE): {
					qxmms_bitrate();
					break;
				}
				case(SWITCH_FILENAME): {
					qxmms_filename();
					break;
				}
				case(SWITCH_PLAYLIST_POS): {
					qxmms_playlist_pos();
					break;
				}
				case(SWITCH_CHANNEL_MODE): {
					qxmms_channels();
					break;
				}
				case(SWITCH_CURRENT_TIME): {
					qxmms_current_time();
					break;
				}
				case(SWITCH_SONG_LENGTH): {
					qxmms_track_length();
					break;
				}
				case(SWITCH_RUN_STATE): {
					qxmms_run_state();
					break;
				}
				case(SWITCH_CURRENT_VOLUME): {
					qxmms_volume();
					break;
				}
				default: {
					printf("[INVALID: %c]",args[switchblock][a]);
					break;
				}
			}
			
			//. end quote.
			if(r_quotes) { printf("\""); }
		}
		
		//. end of line.
		printf("\n");
	} else {

		//. default output for no switches passed.
		printf("%s\n",q.title);
	}

}

void qxmms_play() {
	if(xmms_remote_is_playing(XMMS_SESSION)) {
		qxmms_pause();
	} else {
		xmms_remote_play(XMMS_SESSION);
	}
}

void qxmms_stop() {
	xmms_remote_stop(XMMS_SESSION);
}

void qxmms_pause() {
	xmms_remote_pause(XMMS_SESSION);
}

void qxmms_next() {
	xmms_remote_playlist_next(XMMS_SESSION);
}

void qxmms_prev() {
	xmms_remote_playlist_prev(XMMS_SESSION);
}

void qxmms_addfile(char *string) {
	xmms_remote_playlist_add_url_string(XMMS_SESSION,string);
}

void qxmms_set_volume(char *value) {
	int vol,intval;
	
	xmms_remote_get_volume(XMMS_SESSION, &vol, &vol);

	int a = 1;
	int b = 0;
	char charint[4];	
	if(!strncmp("+",value,1)) {
		while(a < strlen(value) && (value[a] >= '0' && value[a] <= '9')) {
			charint[b++] = value[a++];
		} intval = vol + atoi(charint);
		
		if(intval > 100) { intval = 100; }
	
	} else if(!strncmp("-",value,1)){
		while(a < strlen(value) && (value[a] >= '0' && value[a] <= '9')) {
			charint[b++] = value[a++];
		} intval = vol - atoi(charint);
		
		if(intval < 0) { intval = 0; }	
	
	} else if(!strcmp("down",value)) {
		intval = vol - 5;
	} else if(!strcmp("up",value)) {
		intval = vol + 5;
	} else {
		intval = atoi(value);
	}
	xmms_remote_set_volume(0, intval, intval);
}

void qxmms_mute() {
	FILE *fp;
	int vol;
	char *filename, charint[4];

	filename = (char *)malloc(strlen(getenv("HOME")) + strlen("/.qxmms-mute") + 1);
	memset(filename,0,(strlen(getenv("HOME")) + strlen("/.qxmms-mute") + 1));
	strcat(filename,getenv("HOME"));
	strcat(filename,"/.qxmms-mute");
	
	fp = fopen(filename,"r");
	if(fp) {
		vol = fgetc(fp);
		fclose(fp);
		remove(filename);
		
		if(vol > 100) { vol = 100; }
		else if(vol < 0) { vol = 0; }
		snprintf(charint,4,"%d",vol);
		
		qxmms_set_volume(charint);		
	} else {	
		xmms_remote_get_volume(XMMS_SESSION, &vol, &vol);

		fp = fopen(filename,"w");
		fputc(vol,fp);
		fclose(fp);
		
		qxmms_set_volume("0");
	}
	
	free(filename);
}

void qxmms_jump_pos(char *charvalue) {
	int value = atoi(charvalue);
	xmms_remote_set_playlist_pos(XMMS_SESSION,(value - 1));
}

void qxmms_seek(char *input) {
	
	char *subcmd;
	int cmdlen = strlen(input);
	int curtime = xmms_remote_get_output_time(XMMS_SESSION);
	int inc = 5;
	int min,sec,newtime;

	if(input) {
		if(!strncmp(input,"back",4)) {
			subcmd = strstr(input," ");
			if(subcmd) {
				subcmd++;
				inc = atoi(subcmd);
			}
			xmms_remote_jump_to_time(XMMS_SESSION,curtime - (inc*1000));
		} else if(!strncmp(input,"forward",7)) {
			subcmd = strstr(input," ");
			if(subcmd) {
				subcmd++;
				inc = atoi(subcmd);
			}
			xmms_remote_jump_to_time(XMMS_SESSION,curtime + (inc*1000));
		} else {
			strtok(input,":");
			subcmd = &input[strlen(input) + 1];
			xmms_remote_jump_to_time(XMMS_SESSION,(((atoi(input) * 60) + atoi(subcmd)) * 1000));
		}	
	}
	
}

void qxmms_skin(char *input) {
	xmms_remote_set_skin(XMMS_SESSION, input);
}
